<?php
namespace Common\Model;
use Common\Model\CommonModel;
class RequireModel extends CommonModel
{

	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
		array('type_id', [1,2,3], '分类ID错误！1为普通用户需求，2为商户需求，3为技师需求！', 1, 'in', CommonModel:: MODEL_BOTH  ),
		array('user_id', 'require', '用户必须登录 ！', 1, 'regex', CommonModel:: MODEL_BOTH  ),
		array('title', 'require', '标题不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH  ),
		array('content', 'require', '内容不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('contact', 'require', '联系人姓名不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('phone', 'require', '联系人电话不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('lat', 'require', '维度不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('lng', 'require', '经度不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('status', [0,1], '状态为0或者1！', 2, 'in', CommonModel:: MODEL_BOTH ),
	);
	
	protected $_auto = array(
	    array('create_time','time',CommonModel:: MODEL_INSERT,'function'),
	);

	protected function _after_select(&$data) {
		parent::_after_select($data);

		foreach ($data as &$value){
            $value['avatar']=get_user_avatar($value['avatar']);
            $value['avatar_small']=get_user_avatar($value['avatar']);
            $value['images']=analysisImages($value['images']);
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
            $value['job_type']=self::get_job_type($value);
        }
        unset($value);
	}

	protected function _after_find(&$data) {
		parent::_after_find($data);
		
		$data['avatar']=get_user_avatar($data['avatar']);
        $data['avatar_small']=get_user_avatar($data['avatar']);
        $data['images']=analysisImages($data['images']);
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $data['complain_images']=analysisImages($data['complain_images']);
        $data['bussiness_logo']=sp_get_image_url($data['bussiness_logo']);
        $data['job_type']=self::get_job_type($data);
	}

	protected function _before_write(&$data) {
		parent::_before_write($data);
		//dump($data);exit;
		if(!empty($data['extra'])){
			$data['extra']=json_encode(["type"=>$data['extra']]);
		}
	}

	private function get_job_type($value)
	{
		//dump($value);exit;
		$info=[1=>"机械/电子",2=>"机械",3=>"电子"];
		if ($value['type_id']>1) {
			$extra=json_decode($value['extra'],true);
			return $info[$extra['type']];
		}
		return "";
	}
	
}

